package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.keldian.mobilesafe.BuildConfig;
import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.utils.SharePrefenceTool;

/**
 * Created by Administrator on 16-2-1.
 */
public class DragViewActivity extends Activity {

    private ImageView ivDrag;

    private int startX;
    private int startY;

    private int mScreenWidth;
    private int mScreenHeight;

    private TextView tvTop;

    private TextView tvBottom;

    private float scaleWidth = 1;
    private float scaleHeight = 1;
    private Bitmap bmp;
    private int id = 0;
    private RelativeLayout mRelativeLayout;
    private WindowManager wm;


    long[] mHits = new long[2];// 数组长度多少,就点多少下

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drag_view);
        tvTop = (TextView) findViewById(R.id.tv_top);
        tvBottom = (TextView) findViewById(R.id.tv_bottom);

        bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.drag);
        if (BuildConfig.DEBUG) Log.d("DragViewActivity", bmp + "");
        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        mScreenWidth = wm.getDefaultDisplay().getWidth();// 屏幕宽度
        mScreenHeight = wm.getDefaultDisplay().getHeight();// 屏幕高度

        ivDrag = (ImageView) findViewById(R.id.iv_drag);

        int lastX = SharePrefenceTool.getInt("lastX", 0, this);// left
        int lastY = SharePrefenceTool.getInt("lastY", 0, this);// top

        // 根据布局位置,更新提示框位置
        if (lastY > mScreenHeight / 2) {// 屏幕下方
            tvTop.setVisibility(View.VISIBLE);
            tvBottom.setVisibility(View.INVISIBLE);
        } else {
            tvTop.setVisibility(View.INVISIBLE);
            tvBottom.setVisibility(View.VISIBLE);
        }

        System.out.println("lastX:" + lastX);
        System.out.println("lastY:" + lastY);

        // measure(测量)->layout(设置位置)->onDraw(绘制), 这些方法必须在oncreate方法执行完了之后才会调用
        // ivDrag.layout(lastX, lastY, lastX + ivDrag.getWidth(),
        // lastY + ivDrag.getHeight());//此处不能直接修改布局位置,因为布局还没有开始绘制

        // 获取当前控件的布局参数
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivDrag
                .getLayoutParams();// 父控件就谁,就拿谁定义的布局参数
        // 临时修改布局参数
        params.topMargin = lastY;
        params.leftMargin = lastX;
        // System.out.println("topMargin=" + topMargin);

        ivDrag.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:// 按下动作
                        // System.out.println("按下");

                        // 记录起点坐标
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:// 移动动作
                        // System.ou                        t.println("移动");

                        // 获取移动后坐标
                        int endX = (int) event.getRawX();
                        int endY = (int) event.getRawY();

                        // 计算偏移量
                        int dx = endX - startX;
                        int dy = endY - startY;

                        // 根据偏移量,更新位置
                        int l = ivDrag.getLeft() + dx;
                        int t = ivDrag.getTop() + dy;
                        int r = ivDrag.getRight() + dx;
                        int b = ivDrag.getBottom() + dy;

                        // 避免布局 超出屏幕边界
                        if (l < 0 || r > mScreenWidth) {
                            return true;
                        }
                        // 避免布局 超出屏幕边界
                        if (t < 0 || b > mScreenHeight - 20) {// 减掉状态栏高度
                            return true;
                        }

                        // 根据布局位置,更新提示框位置
                        if (t > mScreenHeight / 2) {// 屏幕下方
                            tvTop.setVisibility(View.VISIBLE);
                            tvBottom.setVisibility(View.INVISIBLE);
                        } else {
                            tvTop.setVisibility(View.INVISIBLE);
                            tvBottom.setVisibility(View.VISIBLE);
                        }

                        ivDrag.layout(l, t, r, b);

                        // 重新初始化起点坐标
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:// 抬起动作
                        // System.out.println("抬起");
                        // 记录最终坐标点
                        SharePrefenceTool.putInt("lastX", ivDrag.getLeft(),
                                getApplicationContext());
                        SharePrefenceTool.putInt("lastY", ivDrag.getTop(),
                                getApplicationContext());
                        break;

                    default:
                        break;
                }

                return false;// 返回true表示消费此事件, 事件不会再进行传递,
                // 当onclick事件和onTouch事件同时设定时,
                // 需要返回false,才可以同时响应两个事件
            }
        });


        ivDrag.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // 参1:原数组, 参2:原数组拷贝位置, 参3:目标数组,参4:目标拷贝位置,参5:拷贝长度
                System.arraycopy(mHits, 1, mHits, 0, mHits.length - 1);
                mHits[mHits.length - 1] = SystemClock.uptimeMillis();// 已开机时间
                if ((SystemClock.uptimeMillis() - mHits[0]) <= 500) {
                    // 双击事件, 双击居中
                    ivDrag.layout(mScreenWidth / 2 - ivDrag.getWidth() / 2,
                            ivDrag.getTop(),
                            mScreenWidth / 2 + ivDrag.getWidth() / 2,
                            ivDrag.getBottom());


//                    small();

                }
            }
        });


    }

    /* 图片缩小的method */
    private void small() {
        int bmpWidth = ivDrag.getWidth();
        int bmpHeight = ivDrag.getHeight();

        /* 设置图片缩小的比例 */
        double scale = 2.0;
        /* 计算出这次要缩小的比例 */
        scaleWidth = (float) (scaleWidth * scale);
        scaleHeight = (float) (scaleHeight * scale);
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth,
                bmpHeight, matrix, true);

        WindowManager.LayoutParams params = (WindowManager.LayoutParams) ivDrag
                .getLayoutParams();// 父控件就谁,就拿谁定义的布局参数

        params.width = bmp.getWidth();
        params.height = bmp.getHeight();


        wm.updateViewLayout(ivDrag, params);
        /* 产生新的ImageView，放入reSize的Bitmap对象，再放入Layout中 */

    }
}
